<template>
  <el-drawer direction="rtl"  :title="dialogTitle" v-model="dialogVisible" size="1200px">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="楼栋地址">
            {{netGroupPayHouseMapData.houseName}}
          </el-form-item>
          <el-form-item label="" prop="keyWord">
            <el-input placeholder="请输入内容" v-model.trim="keyWord">
              <template #prepend>
                <el-select v-model="kType" placeholder="请选择" class="!w-120px">
                  <el-option label="宽带账号" value="netAccount" />
                </el-select>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button
              type="warning"
              plain
              @click="handleImport"
              v-hasPermi="['zwsj:net-group-pay-house-map:import']"
            >
              <Icon icon="ep:upload" /> 导入
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="编号" align="center" prop="id" width="60"/>
          <el-table-column label="账号" align="left" prop="netAccount" width="120"/>
          <el-table-column label="密码" align="center" prop="passWord" width="80"/>
          <el-table-column label="套餐名称" align="left" prop="serverName" width="210"/>
          <el-table-column label="价格" align="left" prop="serverPrice" width="60"/>
          <el-table-column label="地址" align="left" prop="addrRemark" min-width="300"/>
          <el-table-column label="有效期（计费系统）" align="left" width="150">
            <template #default="scope">
              <div>{{ formatDate(scope.row.expireTime) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="80px">
            <template #default="scope">
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['zwsj:net-group-pay-house-map:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
  </el-drawer>
  <!-- 导入对话框 -->
  <NetGroupPayAccountMapImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { NetGroupPayAccountMapApi, NetGroupPayAccountMapVO } from '@/api/zwsj/netgrouppayaccountmap'
import { NetGroupPayHouseMapApi, NetGroupPayHouseMapVO } from '@/api/zwsj/netgrouppayhousemap'
import NetGroupPayAccountMapImportForm from './NetGroupPayAccountMapImportForm.vue'

/** 宽带账号分账 列表 */
defineOptions({ name: 'NetGroupPayAccountMap' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

const loading = ref(true) // 列表的加载中
const list = ref<NetGroupPayAccountMapVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  groupPayHouseMapId: 0,
  netAccount: '',
})

const keyWord = ref('')
const kType = ref('netAccount')


const initKeyWord = async () =>{
  queryParams.netAccount = ''

  if ('netAccount' == kType.value) {
    queryParams.netAccount = keyWord.value
  }
}

/** 打开弹窗 */
const open = async (groupPayHouseMapId: number) => {
  dialogVisible.value = true
  dialogTitle.value = '楼栋账号管理'
  queryParams.groupPayHouseMapId = groupPayHouseMapId

  //查询楼栋地址
  getHouseMapData(groupPayHouseMapId)
  //查询楼栋下账户信息
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const netGroupPayHouseMapData = ref({
  houseId: undefined,
  houseName: undefined,
})
const getHouseMapData = async (groupPayHouseMapId: number) =>{
  const data = await NetGroupPayHouseMapApi.getNetGroupPayHouseMap(groupPayHouseMapId)
  netGroupPayHouseMapData.value = data
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    initKeyWord()
    const data = await NetGroupPayAccountMapApi.getNetGroupPayAccountMapPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await NetGroupPayAccountMapApi.deleteNetGroupPayAccountMap(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}


/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open(queryParams.groupPayHouseMapId)
}

</script>